import logging
import os

from appium import webdriver

from HomeWork_PO_Apptest_add_and_delete_members.base.base_page import BasePage
from HomeWork_PO_Apptest_add_and_delete_members.page.main_page import MainPage


class App(BasePage):
    def start(self):
        #从系统中获取 设备id 和 端口号
        #udid = os.getenv("udid")
        #port = os.getenv("port")
        #添加日志信息
        #logging.info(f"传入参数===>>>udid:{udid},port:{port}")
        #先判断当前driver是否为空，如果为空，就加个driver;如果不为空，就复用driver
        if self.driver == None:
            desired_cap = {
                "platformName": "android",
                "deviceName": "127.0.0.1:7555",
                "appPackage": "com.tencent.wework",
                "appActivity": "com.tencent.wework.launch.WwMainActivity",
                "noReset": True,

                "unicodeKeyBoard": True,
                "resetKeyBoard": True,
            }
            self.driver = webdriver.Remote(f"http://127.0.0.1:4723/wd/hub", desired_cap)
            self.driver.implicitly_wait(15)
        else:
            logging.info(f"复用driver")
            self.driver.launch_app()
        return self

    def stop(self):
        self.driver.quit()

    def restart(self):
        self.driver.close_app()
        self.driver.launch_app()

    def goto_main(self):
        #程序入口
        print(self.driver)
        return MainPage(self.driver)